---
title: "Experiment 3c"
author: "Jonathan Phillips & Regan Bernhard"
date: "4/21/2022"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r clean_data}
library(tidyverse)

d <- read.csv("exp3_rawdata.csv")

a <- d

## need to get the list of columns with this in the name: comp3a

compAs <- grep("comp3",names(d))

d <- a %>% select(-c(1:8,10:17,all_of(compAs))) %>% 
  pivot_longer(2:1729, "question","response",values_drop_na=T) %>%
  filter(bot1==1 & bot2==2) %>%
  separate(question,into = c("scenario","options","orderInfo","question","XX"),sep="_") %>%
  mutate(scenario = as.numeric(gsub("X","",scenario))
         ,altQuality = case_when(
           str_detect(options,"LLH") ~ "Normal Alternative"
           ,str_detect(options,"LHH") ~ "Abnormal Alternative"
         )
         ,questionKind = case_when(
           str_detect(question,"force") ~ "hadTo"
           ,str_detect(question,"norm") ~ "unusualness"
           ,str_detect(question,"val") ~ "goodIdea"
           ,str_detect(question,"comp1") ~ "comprehension1"
           ,str_detect(question,"comp2") ~ "comprehension2"
         )
         ,target = case_when(
            str_detect(question,"actnorm") ~ "actual"
           ,str_detect(question,"actval") ~ "actual"
           ,str_detect(question,"norm1") ~ "alt1"
           ,str_detect(question,"norm2") ~ "alt2"
           ,str_detect(question,"val1") ~ "alt1"
           ,str_detect(question,"val2") ~ "alt2"
         )
         ) %>%
  select(-c(bot1,bot2,options:XX)) %>%
  pivot_wider(names_from = c(questionKind,target),values_from=value) %>%
  filter(comprehension1_NA==2 & comprehension2_NA==3) %>%
  rowwise() %>% ## Min/Max works here because it just takes the one value for the actual
  mutate(Value = max(goodIdea_alt1,goodIdea_alt2,goodIdea_actual,na.rm = T)
         ,Norm = min(unusualness_alt1,unusualness_alt2,unusualness_actual,na.rm=T)
        ) %>%
  rename(Force=hadTo_NA) 

d$Norm[d$Norm==Inf] <- NA # one sub didn't give a value for norm resulting in Inf from the Min

#only keep participants with complete data
d <- d %>% drop_na(Norm)
d <- d %>% drop_na(Value)

d$altQuality <- factor(d$altQuality, levels = c("Normal Alternative", "Abnormal Alternative"))

d.all <- d

d <- d.all %>% dplyr::select(-c(age:edu,goodIdea_actual:unusualness_alt1))

write.csv(d, "exp3c_cleandata.csv")

```



```{r descriptives}
library(plyr)

#get descriptives
force_bycond_desc <- ddply(d, c("condition", "altQuality"), summarise,
               N    = length(Force),
               mean = mean(Force),
               sd   = sd(Force),
               se   = sd / sqrt(N)
)
print(force_bycond_desc)

force_desc <- ddply(d, c("altQuality"), summarise,
               N    = length(Force),
               mean = mean(Force),
               sd   = sd(Force),
               se   = sd / sqrt(N)
)
print(force_desc)

val_desc <- ddply(d, c("condition", "altQuality"), summarise,
               N    = length(Value),
               mean = mean(Value),
               sd   = sd(Value),
               se   = sd / sqrt(N)
)
print(val_desc)

norm_desc <- ddply(d, c("condition", "altQuality"), summarise,
               N    = length(Norm),
               mean = mean(Norm),
               sd   = sd(Norm),
               se   = sd / sqrt(N)
)
print(norm_desc)

character_desc <- ddply(d, c("altQuality"), summarise,
               N    = length(character_1),
               mean = mean(character_1),
               sd   = sd(character_1),
               se   = sd / sqrt(N)
)
print(character_desc)

character_bycond_desc <- ddply(d, c("condition", "altQuality"), summarise,
               N    = length(character_1),
               mean = mean(character_1),
               sd   = sd(character_1),
               se   = sd / sqrt(N)
)
print(character_bycond_desc)

```
## Predicting force from quality of the altnerative
```{r basic analyses}

#predicting force whether or not there was a normal alternative
summary(lm(Force ~ altQuality, data = d))

#predicting character judgments by whether or not there was a normal alternative
summary(lm(character_1 ~ altQuality, data = d))

#predicting preceptions of the actual actions by condition
summary(lm(Norm ~ altQuality, data = d[d$condition == "Actual",]))
summary(lm(Value ~ altQuality, data = d[d$condition == "Actual",]))

```

## Testing whether condition affects evaluations of the actual action/character
```{r basic analyses}
alt_data <- d[d$condition == "Alternative",]
act_data <- d[d$condition == "Actual",]

mosaic::cor.test(Value ~ Norm, data = alt_data)

#Are the actual normality judgments in both conditions significantly below 50?
#In other words, are the actual actions always viewed as very normal? 
mosaic::t.test(~Norm, mu = 50, data = act_data[act_data$altQuality == "Normal Alternative",])
mosaic::t.test(~Norm, mu = 50, data = act_data[act_data$altQuality == "Abnormal Alternative",])

#Are the actual value judgments in both conditions significantly above 50?
#In other words, are the actual actions always viewed as very good? 
mosaic::t.test(~Value, mu = 50, data = act_data[act_data$altQuality == "Normal Alternative",])
mosaic::t.test(~Value, mu = 50, data = act_data[act_data$altQuality == "Abnormal Alternative",])

#Are the character judgments in both conditions significantly above 50?
mosaic::t.test(~character_1, mu = 50, data = d[d$altQuality == "Normal Alternative",])
mosaic::t.test(~character_1, mu = 50, data = d[d$altQuality == "Abnormal Alternative",])
```


```{r basic analyses}

#predicting force from norm and value in the alternative condition
alt_mod <- (lm(Force ~ Value + Norm, data = d[d$condition == "Alternative",]))
summary(alt_mod)

#including alternative quality
summary(lm(Force ~ Value + Norm + altQuality, data = d[d$condition == "Alternative",]))

#predict force from just altNorm and just altVal
summary(lm(Force ~ Norm, data = d[d$condition == "Alternative",]))
summary(lm(Force ~ Value, data = d[d$condition == "Alternative",]))

#predicting force from norm and value in the actual condition
summary(lm(Force ~ Value + Norm, data = d[d$condition == "Actual",]))
#including alternative quality
summary(lm(Force ~ Value + Norm + altQuality, data = d[d$condition == "Actual",]))



```

###Final graphs
```{r final graphs}

#Import study 3a and 3b data and prepare all three datasets to be merged.
a_data <- read.csv("exp3a_cleandata.csv")
b_data <- read.csv("exp3b_cleandata.csv")

#select only the columns I want to keep from study 3a
a_data_mergable <- a_data %>% dplyr::select(-c(X,ResponseId,scenario,Force,comprehension1_NA:altNorm))
#rename the norm and value columns to match across studies
a_data_mergable <- rename(a_data_mergable, Value = actValue)
a_data_mergable <- rename(a_data_mergable, Norm = actNorm)
#create a new study number variable name
a_data_mergable$study_num <- "Experiment 3a"

#secelct only the columns and condition I want to keep from study 3b
b_data_mergable <- b_data[b_data$condition == "Actual",] %>% dplyr::select(-c(X:bot2,age:scenario,Force,goodIdea_actual:unusualness_alt1))
#create a new study number variable name
b_data_mergable$study_num <- "Experiment 3b"

c_data_mergable <- d[d$condition == "Actual",] %>% dplyr::select(-c(ResponseId,condition, scenario,Force,unusualness_actual))
#create a new study number variable name
c_data_mergable$study_num <- "Experiment 3c"

merged_data <- rbind(a_data_mergable, b_data_mergable, c_data_mergable)


cond_legend_title<-"Alternative Action"
study3_fig3_value<-merged_data %>%
  ggplot(aes(study_num, Value, fill=altQuality)) +
  geom_bar(stat="summary", fun.y='mean', position='dodge') +
  geom_errorbar(stat='summary', width=.2,
                 position=position_dodge(.9)) +
  geom_point(aes(study_num, color=altQuality), size=2,
             position=position_jitterdodge(dodge.width=.9,jitter.width = .25), alpha = .5) +
             scale_color_manual(cond_legend_title, values = c("Normal Alternative" = "#8a433e", "Abnormal Alternative" = "#017073")) +
  scale_fill_manual(cond_legend_title, values = c("Normal Alternative" = "#F8766D", "Abnormal Alternative" = "#00BFC4")) +
  xlab("") +
  ylab(str_wrap("How strongly do you agree that the actual action was a good thing to do?", 75)) +
  scale_y_continuous(breaks=waiver(), labels = str_wrap(c("Strongly Disagree","25","50","75","Strongly Agree"),10)) + 
  theme_bw() +
  theme(strip.text = element_text(size=14),
        axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title = element_text(size=20, vjust=0.35),
        panel.grid = element_blank(),
        legend.position = "none")

ggsave("study3_fig3_value.jpg",height = 10, width= 7, dpi=600)

cond_legend_title<-"Alternative Action"
study3_fig3_norm<-merged_data %>%
  ggplot(aes(study_num, Norm, fill=altQuality)) +
  geom_bar(stat="summary", fun.y='mean', position='dodge') +
  geom_errorbar(stat='summary', width=.2,
                 position=position_dodge(.9)) +
  geom_point(aes(study_num, color=altQuality), size=2,
             position=position_jitterdodge(dodge.width=.9,jitter.width = .25), alpha = .5) +
             scale_color_manual(cond_legend_title, values = c("Normal Alternative" = "#8a433e", "Abnormal Alternative" = "#017073")) +
  scale_fill_manual(cond_legend_title, values = c("Normal Alternative" = "#F8766D", "Abnormal Alternative" = "#00BFC4")) +
  xlab("") +
  ylab(str_wrap("How strongly do you agree that the actual action was unusual?", 75)) +
  scale_y_continuous(breaks=waiver(), labels = str_wrap(c("Strongly Disagree","25","50","75","Strongly Agree"),10)) + 
  theme_bw() +
  theme(strip.text = element_text(size=14),
        axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title = element_text(size=20, vjust=0.35),
        panel.grid = element_blank(),
        legend.position = "none")

ggsave("study3_fig3_norm.jpg",height = 10, width= 7, dpi=600)

cond_legend_title<-"Alternative Action"
study3_fig3_char<-merged_data %>%
  ggplot(aes(study_num, character_1, fill=altQuality)) +
  geom_bar(stat="summary", fun.y='mean', position='dodge') +
  geom_errorbar(stat='summary', width=.2,
                 position=position_dodge(.9)) +
  geom_point(aes(study_num, color=altQuality), size=2,
             position=position_jitterdodge(dodge.width=.9,jitter.width = .25), alpha = .5) +
             scale_color_manual(cond_legend_title, values = c("Normal Alternative" = "#8a433e", "Abnormal Alternative" = "#017073")) +
  scale_fill_manual(cond_legend_title, values = c("Normal Alternative" = "#F8766D", "Abnormal Alternative" = "#00BFC4")) +
  xlab("") +
  ylab(str_wrap("How strongly do you agree that the contestant is the kind of person who makes good decisions?", 75)) +
  scale_y_continuous(breaks=waiver(), labels = str_wrap(c("Strongly Disagree","25","50","75","Strongly Agree"),10)) + 
  theme_bw() +
  theme(strip.text = element_text(size=14),
        axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title = element_text(size=20, vjust=0.35),
        panel.grid = element_blank(),
        legend.position = "none")

ggsave("study3_fig3_char.jpg",height = 10, width= 7, dpi=600)

```

